package com.hl.card.modular.packBack.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.HttpURLClient;
import com.hl.card.common.util.ResponseData;
import com.hl.card.modular.packBack.service.VipManageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class VipManageServiceImpl implements VipManageService {
    //支付中心接口的地址
    @Value("${dismemberapply.pay.baseUrl}")
    private String baseUrl;

    @Autowired
       private ResponseData responseData;

    @Autowired
       private VipManageService vipManageService;

    @Autowired
      private UserMapper userMapper;
       @Value("${dismemberapply.ka_ip}")
       private String ip;  //卡包项目IP
@Override
public JSONObject queryVipManage(String userId,Integer start, Integer end, String createTime, String userName, String loginAccount,
       String remainPart,String ismember, String freezePart) {
   Map<String,String> paramMap=new HashMap<String, String>();
   paramMap.put("start", start+"");
   paramMap.put("end", end+"");
       if(createTime!=null&&createTime!=""){
          paramMap.put("createTime", createTime);
          }
          if(userName!=null&&userName!=""){
              paramMap.put("userName", userName);
              }
          if(loginAccount!=null&&loginAccount!=""){
               paramMap.put("loginAccount", loginAccount);
              }
          if( remainPart!=null&&remainPart!=""){
              paramMap.put("remainPart", remainPart);
              }
          if(freezePart!=null&&freezePart!=""){
              paramMap.put("freezePart", freezePart);
              }
       if(ismember!=null&&ismember!=""){
              paramMap.put("ismember", ismember);
              }
    String result= HttpURLClient.post(paramMap, baseUrl + "/api/saCard/list");

    JSONObject json = JSONObject.parseObject(result);
    JSONArray res = json.getJSONArray("list");
    JSONArray array=new JSONArray();
   for (int i = 0; i < res.size(); i++) {
       JSONObject obj=new JSONObject();
       String id = res.getJSONObject(i).getString("userId") ;
       Map<String, Object> map = new HashMap<>();
       map.put("userId",id);
       map.put("ismember",ismember);
       List<User> list= vipManageService.queryVipManage(map);
       if(list.size() > 0) {
           String imb = list.get(0).getSecret();
           res.getJSONObject(i).put("ismember", imb);
       }
   }
    String member=null;
    if (ismember==null) {
        member="";
    }else {
    if (ismember.equals("0")) {
            member="会员";
   }if (ismember.equals("1")) {
            member="合伙人";
       }
   }
   for (int i = 0; i < res.size(); i++) {
       if(StringUtils.isBlank(ismember)){
           JSONObject jo=res.getJSONObject(i);
           array.add(jo);
       }else if(member.equals(res.getJSONObject(i).getString("ismember"))){
               JSONObject jo=res.getJSONObject(i);
               array.add(jo);
           }
   }
    responseData.setMsg("操作成功");
    responseData.setStatus("1");
   json.put("list",array);
   return json;
}
@Override
public List<User> queryVipManage(Map<String, Object> map) {
    List<User> list=	userMapper.queryVipManage(map);
   return list;
}

}
